package com.rimi.lyingGoShoppingMall.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rimi.lyingGoShoppingMall.bean.Product;
import com.rimi.lyingGoShoppingMall.commos.ResponseData;
import com.rimi.lyingGoShoppingMall.commos.Status;
import com.rimi.lyingGoShoppingMall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.swing.plaf.nimbus.State;
import java.util.List;

/**
 * @author 61919
 * @date 2020/3/3
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;



    /**
     * 显示所有商品的视图页面
     * @return
     */
    @GetMapping("productPage")
    public String productPage(){
        return "product/productPage";
    }
    /**
     * 将所有商品的信息传送给页面
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/allProduct",produces = "application/json")
    public ResponseData productList(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Product> allProduct = productService.findAllProducts();
        return ResponseData.onSuccess(PageInfo.of(allProduct));
    }



    /**
     * 显示添加商品的视图
     */
    @GetMapping("addProductPage")
    public String addProductPage(){
        return "product/addProductPage";
    }
    /**
     * 将添加商品的数据从前端传入后台
     */
    @ResponseBody
    @PostMapping(value = "/addProductPage",produces = "application/json")
    public ResponseData addProduct(Product product){
        return productService.addProducts(product) ? ResponseData.onSuccess() : ResponseData.onFailed(Status.ADD_PRODUCT_FAILED);
    }

}
